/**
 * com.dingjian.basedata.controller.OrgController.java
 */
package com.dingjian.basedata.org.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dingjian.base.CoreEntity;
import com.dingjian.base.service.BaseService;
import com.dingjian.base.util.StringUtils;
import com.dingjian.basedata.org.enums.OrgStatusEnum;
import com.dingjian.basedata.org.model.Org;
import com.dingjian.basedata.org.model.Position;
import com.dingjian.basedata.org.service.OrgService;
import com.dingjian.basedata.org.service.PositionService;
import com.dingjian.framework.base.controller.TreeListController;

/**
 * @author FengMy
 * 
 * @since 2012-9-7
 */
@Controller
@RequestMapping(value="basedata/org/*")
public class OrgListController extends TreeListController {
	@Autowired
	private OrgService orgService;
	@Autowired
	private PositionService positionService;
	
	@Override
	protected CoreEntity createNewEntity() {
		String parentId = getString("parent");
		Org org = new Org();
		if(!StringUtils.isEmpty(parentId)){
			Org parent = orgService.getEntityById(parentId);
			org.setParent(parent);
		}
		return org;
	}
	
	@Override
	protected String getListView() {
		return "basedata/org/orgList";
	}

	@Override
	protected String getEditView() {
		return "basedata/org/orgEdit";
	}

	@Override
	protected String getListMapper() {
		return "com.dingjian.basedata.org.dao.OrgDao.select";
	}

	@Override
	protected BaseService<Org> getService() {
		return orgService;
	}

	@Override
	protected String getTreeDataMapper() {
		return "com.dingjian.basedata.org.dao.OrgDao.getChild";
	}
	
	/**
	 * ${base}/basedata/org/orgDataPicker?multi=true为多选
	 * @param model
	 * @return
	 */
	@RequestMapping(value="orgDataPicker")
	public String orgDataPicker(ModelMap model){
		Map<String,String> param = getParamMap();
		Set<String> keys = param.keySet();
		for(String s : keys){
			model.put(s, param.get(s));
		}
		return "basedata/org/orgDataPicker";
	}
	
	@RequestMapping(value="positionSetting")
	public String positionSetting(ModelMap model){
		String orgId = getString("org");
		Org org = orgService.getEntityById(orgId);
		model.put("org", org);
		List<Position> positions = positionService.getByOrg(orgId);
		model.put("positions", positions);
		return "basedata/org/positionSetting";
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value="positionSave")
	public void positionSave(HttpServletResponse response){
		String orgId = getString("org.id");
		String posJson = getString("positionJson");
		if(!StringUtils.isEmpty(orgId)){
			Collection<Position> posColl = JSONArray.toCollection(JSONArray.fromObject(posJson), Position.class);
			List<Position> positions = new ArrayList<Position>(posColl);
			Org org = new Org();
			org.setId(orgId);
			for(Position p : positions){
				p.setBelongOrg(org);
			}
			positionService.savePositions(positions);
		}
		getOutputMsg().put("STATE", "SUCCESS");
		getOutputMsg().put("MSG", "保存成功");
		outPrint(response, JSONObject.fromObject(getOutputMsg()).toString());
	}

	@Override
	protected String getSimpleTreeDataMapper() {
		return "com.dingjian.basedata.org.dao.OrgDao.getSimpleTreeData";
	}
	
	@Override
	protected boolean isAllowDelete(CoreEntity entity) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("parent", entity.getId());
		if(queryExecutor.execCount("com.dingjian.basedata.org.dao.OrgDao.select", param)>0){
			setOutputMsg("MSG", "存在下级组织,不能删除");
			return false;
		}
		if(!OrgStatusEnum.CREATE.equals(((Org)entity).getStatus())){
			setOutputMsg("MSG", "只能删除创建状态下的组织");
			return false;
		}
		return super.isAllowDelete(entity);
	}
}
